<%--
  Copyright � 2005 Obeo.
  All rights reserved.   This program and the accompanying materials
  are made available under the terms of the GNU Public License v2.0 or any later version
  which accompanies this distribution, and is available at
  http://www.gnu.org/licenses/gpl.txt
--%>
<%
metamodel http://www.eclipse.org/uml2/2.0.0/UML

import org.acceleo.modules.uml2.services.Uml2Services
import org.acceleo.modules.uml2.services.StringServices
%>

<%-- 
This template generate Data Objects Access Implementation as recommended in the Sun BluePrint pattern..
--%>

<%script type="Class" name="fullFilePath"%>
<%if (hasStereotype("Entity")) {%>
/<%jdbcPackage.toPath()%>/Jdbc<%name%>Dao.java
<%}%>

<%script type="Class" name="generate" file="<%fullFilePath%>"%>
<%-- DAO implementation generation --%>
package <%jdbcPackage%>;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.acceleo.fwk.dao.DaoException;
import org.acceleo.fwk.dao.jdbc.JdbcConnectionUtils;
import org.acceleo.sample.dto.<%name%>Dto;

/**
 * This class provides the data access layer to the <%name%> entity class.
 * This is the JDBC implementation.
 */
public class Jdbc<%name%>Dao {

<%-- Generates static declaration for database fields --%>
	protected final static String TABLE_NAME = "T_<%name%>";

<%for (attribute){%>
	protected final static String <%name.toUpperCase()%> = "<%name%>";
<%}%>
	
	protected static final String CREATE_QUERY = 
		"INSERT INTO " + TABLE_NAME + 
		" (<%attribute.name.sep(", ")%>) VALUES ( <%for (attribute){%>? <%}%>)";
		
	protected static final String UPDATE_QUERY = 
		"UPDATE " + TABLE_NAME + 
		" SET  <%attribute.genComparator.sep(", ")%>" +
		" WHERE ID = ?";
	
	protected static final String DELETE_QUERY = 
		"DELETE FROM " + TABLE_NAME + 
		" WHERE ID = ?";
		
	protected static final String FIND_ALL_QUERY = 
		"SELECT * FROM " + TABLE_NAME;
		
	protected static final String FIND_BY_ID = 
		"SELECT * FROM " + TABLE_NAME +
		" WHERE ID = ?";

	public void create<%name%>(<%name%>Dto element) throws DaoException {
		Connection cnx = null;
		PreparedStatement statement = null;

		try {
			cnx = JdbcConnectionUtils.getInstance().getConnection();
			statement = cnx.prepareStatement(CREATE_QUERY);

			// <%startUserCode%> create
			<%for (attribute){%>
				<%if (type.name == "String"){%>
			statement.setString(<%i()+1%>, element.get<%name.firstUpper()%>());
				<%}else if(type.name == "Date"){%>
			statement.setDate(<%i()+1%>, new Date(element.get<%name.firstUpper()%>().getTime()));
				<%}else if(type.name == "Integer"){%>
			statement.setInt(<%i()+1%>, element.get<%name.firstUpper()%>());
				<%}%>
			<%}%>
			// <%endUserCode%>

			statement.executeUpdate();
		} catch (SQLException e) {
			throw new DaoException("Erreur requete", e);
		} finally {
			try {
				if(statement!=null){statement.close();}
				if(cnx!=null){cnx.close();}
			} catch (SQLException e) {
				throw new DaoException("Erreur de fermeture JDBC", e);
			}
		}
	}
	
	public void update<%name%>(<%name%>Dto element) throws DaoException {
		Connection cnx = null;
		PreparedStatement statement = null;

		try {
			cnx = JdbcConnectionUtils.getInstance().getConnection();
			statement = cnx.prepareStatement(UPDATE_QUERY);
			// <%startUserCode%> update
			<%for (attribute){%>
				<%if (type.name == "String"){%>
			statement.setString(<%i()+1%>, element.get<%name.firstUpper()%>());
				<%}else if(type.name == "Date"){%>
			statement.setDate(<%i()+1%>, new Date(element.get<%name.firstUpper()%>().getTime()));
				<%}else if(type.name == "Integer"){%>
			statement.setInt(<%i()+1%>, element.get<%name.firstUpper()%>());
				<%}%>
			<%}%>
			// <%endUserCode%>

			statement.executeUpdate();
		} catch (SQLException e) {
			throw new DaoException("Erreur requete", e);
		} finally {
			try {
				if(statement!=null){statement.close();}
				if(cnx!=null){cnx.close();}
			} catch (SQLException e) {
				throw new DaoException("Erreur de fermeture JDBC", e);
			}
		}
	}
	
	public void delete<%name%>(<%name%>Dto element) throws DaoException {
		Connection cnx = null;
		PreparedStatement statement = null;

		try {
			cnx = JdbcConnectionUtils.getInstance().getConnection();
			statement = cnx.prepareStatement(DELETE_QUERY);
			// <%startUserCode%> delete
			<%for (attribute){%>
				<%if (type.name == "String"){%>
			statement.setString(<%i()+1%>, element.get<%name.firstUpper()%>());
				<%}else if(type.name == "Date"){%>
			statement.setDate(<%i()+1%>, new Date(element.get<%name.firstUpper()%>().getTime()));
				<%}else if(type.name == "Integer"){%>
			statement.setInt(<%i()+1%>, element.get<%name.firstUpper()%>());
				<%}%>
			<%}%>
			// <%endUserCode%>


			statement.executeUpdate();
		} catch (SQLException e) {
			throw new DaoException("Erreur requete", e);
		} finally {
			try {
				if(statement!=null){statement.close();}
				if(cnx!=null){cnx.close();}
			} catch (SQLException e) {
				throw new DaoException("Erreur de fermeture JDBC", e);
			}
		}
	}
	
	public List<<%name%>Dto> findAll<%name%>s() throws DaoException {
		Connection cnx = null;
		Statement statement = null;
		ResultSet rs = null;

		try {
			cnx = JdbcConnectionUtils.getInstance().getConnection();
			statement = cnx.createStatement();
			rs = statement.executeQuery(FIND_ALL_QUERY);

			List<<%name%>Dto> result = createList(rs);
			return result;
		} catch (SQLException e) {
			throw new DaoException("Erreur requete", e);
		} finally {
			try {
				if(rs!=null){rs.close();}
				if(statement!=null){statement.close();}
				if(cnx!=null){cnx.close();}
			} catch (SQLException e) {
				throw new DaoException("Erreur de fermeture JDBC", e);
			}
		}
	}	
	
	public <%name%>Dto find<%name%>ById(String id) throws DaoException {
		Connection cnx = null;
		Statement statement = null;
		ResultSet rs = null;

		try {
			cnx = JdbcConnectionUtils.getInstance().getConnection();
			statement = cnx.createStatement();
			rs = statement.executeQuery(FIND_BY_ID);

			<%name%>Dto result = new <%name%>Dto();
			<%for (attribute){%>
				<%if (type.filter("PrimitiveType") != null){%>
			result.set<%name.firstUpper()%>( rs.get<%genRsType%>(<%name.toUpperCase()%>) );
				<%}%>
			<%}%>
			return result;
		} catch (SQLException e) {
			throw new DaoException("Erreur requete", e);
		} finally {
			try {
				if(rs!=null){rs.close();}
				if(statement!=null){statement.close();}
				if(cnx!=null){cnx.close();}
			} catch (SQLException e) {
				throw new DaoException("Erreur de fermeture JDBC", e);
			}
		}
	}
	
	protected List<<%name%>Dto> createList(ResultSet rs) throws SQLException {
		List<<%name%>Dto> result = new ArrayList<<%name%>Dto>();
		<%name%>Dto <%name.firstLower()%> = null;
		while (rs.next()) {
			<%name.firstLower()%> = new <%name%>Dto();
			<%for (attribute){%>
				<%if (type.filter("PrimitiveType") != null){%>
			<%class.name.firstLower()%>.set<%name.firstUpper()%>( rs.get<%genRsType%>(<%name.toUpperCase()%>) );
				<%}%>
			<%}%>
			result.add(<%name.firstLower()%>);
		}
		return result;
	}

	
	
}

<%script type="Class" name="jdbcPackage"%>
org.acceleo.sample.jdbc

<%script type="Property" name="genComparator"%>
<%name%> = ? 

<%script type="Property" name="genRsType"%>
<%type.name.firstUpper()%>
